package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.manager.HbAppManager;
import shuashuami.hb.com.share.ShareCUserInfo;

public class WangjimimaActivity extends AbActivity {

    private EditText edtPhoneNum, edtCOde, edtNewPassword, edtPassowrd;
    private TextView tvGetcode;
    private TextView tvCodeCountDown;

    private int recLen = 60;

    private Button btnEnter;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_wangjimima);
        setLeftView();
        edtPhoneNum = (EditText) findViewById(R.id.edt_bind_phone_num_phone_num);
        edtCOde = (EditText) findViewById(R.id.edt_bind_phone_num_code);
        edtNewPassword = (EditText) findViewById(R.id.edt_bind_phone_num_new_password);
        edtPassowrd = (EditText) findViewById(R.id.edt_bind_phone_num_new_password_enter);
        tvGetcode = (TextView) findViewById(R.id.tv_bind_phone_num_get_code);
        tvCodeCountDown = (TextView) findViewById(R.id.tv_bind_phone_num_count_down);
        btnEnter = (Button) findViewById(R.id.btn_bind_phone_num_next);

    }

    @Override
    protected void initData() {
        setTitleBar("重置密码");
    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvGetcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recLen = 60;
                getCode();
            }
        });
        btnEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chongzhimima();
            }
        });
    }

    private void chongzhimima() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(3);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };

        HttpMethods.chongzhimima(listener,
                edtPhoneNum.getText().toString(), edtCOde.getText().toString()
                , edtNewPassword.getText().toString(), edtPassowrd.getText().toString()
        );
    }

    public void getCode() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };

        HttpMethods.getCode(listener,
                edtPhoneNum.getText().toString(), "reset"
        );
    }

    private void countDown() {
        tvCodeCountDown.setVisibility(View.VISIBLE);
        Message message = handler.obtainMessage(1);     // Message
        handler.sendMessageDelayed(message, 1000);

    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    recLen--;
                    tvCodeCountDown.setText("" + recLen);

                    if (recLen > 0) {
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);      // send message
                    } else {
                        tvCodeCountDown.setVisibility(View.GONE);
                    }
                    break;
                case 2:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(WangjimimaActivity.this, "获取成功");
                            countDown();
                        } else {
                            ToastUtil.showShort(WangjimimaActivity.this, jsonObject.getString("message"));

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;
                case 3:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) {
                            int tag = HbAppManager.getInstance().getTypeValue();
                            ShareCUserInfo shareCUserInfo = new ShareCUserInfo(WangjimimaActivity.this);
                            shareCUserInfo.setPassword(edtNewPassword.getText().toString());
                            setResult(RESULT_CANCELED);
                            finish();
                            ToastUtil.showShort(WangjimimaActivity.this, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(WangjimimaActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;
            }

            super.handleMessage(msg);
        }
    };
}
